package SmallDataset;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import vo.User;
import JDBC.DatabaseDao;
import Utils.Constants;
import Utils.DateUtils;
import Utils.FileUtils;
import Utils.MathUtils;

public class ContentRanking {
	// 计算用户的微博总个数
	public static void computeUserIdStatusCount() {
		String result = "";
		ArrayList<User> users = Init.loadUsers();
		System.out.println("用户数:" + users.size());
		for (User user : users) {
			String userId = user.getUserId();
			int statusesCount = user.getStatusesCount();
			result += userId + Constants.HASH_SEPERATOR + statusesCount + "\n";

		}
		FileUtils.write(Constants.USERID_STATUSCOUNT_PATH, result);
	}

	// 计算用户的微博总长度，单位是字符
	public static void computeUserIdStatusAVGLength() {
		String result = "";
		ArrayList<String> userIds = Init.loadUserIds();
		String ids = "";
		for (String userId : userIds) {
			ids += userId + ",";
		}
		ids = ids.substring(0, ids.length() - 1);
		try {
			String sql = "select user_id, avg(length) from statuses where user_id in(" + ids + ") group by user_id ";
			System.out.println(sql);
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			int count = 0;
			while (rs.next()) {
				String userId = rs.getString("user_id");
				double lengthAVG = MathUtils.precision(rs.getDouble(2), Constants.PRECISION);
				result += userId + Constants.HASH_SEPERATOR + lengthAVG + "\n";
				count++;
			}
			System.out.println("多少个平均值:" + count);
			FileUtils.write(Constants.USERID_STATUSAVGLENGTH_PATH, result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 主要是为了计算单位时间的内容量
	public static void computeUserIdTimeRange() {
		ArrayList<String> userIds = Init.loadUserIds();
		String ids = "";
		for (String userId : userIds) {
			ids += userId + ",";
		}
		ids = ids.substring(0, ids.length() - 1);

		Hashtable<String, Integer> userId_timeRange = new Hashtable<String, Integer>();
		String sql = "select user_id,min(created_at),max(created_at) from statuses where user_id in (" + ids + ") group by user_id";
		try {
			System.out.println(sql);
			Statement stmt = DatabaseDao.getConnection().createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while (rs.next()) {
				String userId = rs.getString(1);
				Date min = rs.getTimestamp(2);
				Date max = rs.getTimestamp(3);
				int timeRange = DateUtils.getDayDistance(max, min);
				userId_timeRange.put(userId, timeRange);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		FileUtils.writeFileHashtable(userId_timeRange, Constants.USERID_STATUS_TIMERANGE_PATH, -1);

	}

	// 计算微博的总内容数
	public static void computeUserIdContent() {
		Hashtable<String, Double> userId_statusesLengthAVG = FileUtils.readHashtableD(Constants.USERID_STATUSAVGLENGTH_PATH);
		Hashtable<String, Double> userId_statusesCounts = FileUtils.readHashtableD(Constants.USERID_STATUSCOUNT_PATH);
		Hashtable<String, Double> userId_timeRange = FileUtils.readHashtableD(Constants.USERID_STATUS_TIMERANGE_PATH);
		Hashtable<String, Double> userId_content = new Hashtable<String, Double>();
		ArrayList<String> userIds = Init.loadUserIds();
		for (String userId : userIds) {
			double statusesAVGLength = userId_statusesLengthAVG.get(userId);
			double statusesCount = userId_statusesCounts.get(userId);
			double timeRange = userId_timeRange.get(userId);
			double content = 0.0;
			if (timeRange == 0)
				content = 0;// 只提供了一个帖子，贡献很小，忽略不计
			else
				content = statusesAVGLength * statusesCount / timeRange;
			userId_content.put(userId, MathUtils.precision(content, Constants.PRECISION));
		}
		FileUtils.writeFileHashtable(userId_content, Constants.USERID_CONTENT_PATH, Constants.DESC);
	}

	public static void main(String[] args) {
		// computeUserIdStatusCount();
		// computeUserIdStatusAVGLength();
		// computeUserIdTimeRange();
		computeUserIdContent();

	}
}
